|
|
|
|
|
|
|
8. Create an array that is big enough to hold the image's pixel information. |
|
|
|
|
|
|
|
|
9. Use the GetDIBits API function to finally copy the image's pixel information into the array. |
|
|
|
|
|
|
|
|
10. Use the DeleteObject API function to delete the new device context and bitmap. |
|
|
|
|
|
|
|
|
At this point the program has copied the pixel information from the picture box into a byte array. You can now rearrange the information to make working with it easier. For example, the previous section shows how to copy the pixel data for a device-dependent bitmap into an array of RGBTriplets for easier manipulation. You can perform a similar operation with the DIB pixel information. |
|
|
|
|
|
|
|
|
Looking at the previous list of steps, you may have noticed that working with DIBs is much more complicated than working with device-dependent bitmaps. Working with DIBs is also generally slower. |
|
|
|
|
|
|
|
|
There are two main reasons DIBs are useful. First, they are the newer technology and support for them will probably improve. Later versions of Windows may someday include faster DIB routines that are easier to use. |
|
|
|
|
|
|
|
|
Second, a program can load a DIB at a color resolution different from its current one. In step 7, the program sets the BITMAPINFOHEADER's biBitCount field to indicate the number of bits per pixel the DIB should have. This need not be the same as the picture box's number of bits per pixel. For instance, the program could load a 24-bit image into an 8-bit DIB and then display it at its new resolution. |
|
|
|
|
|
|
|
|
Thankfully, displaying a DIB is much easier than loading one. The program simply uses the StretchDIBits API function to copy the DIB into the picture box. |
|
|
|
|
|
|
|
|
Example program CopyDIB, shown in Figure 3.3, shows how to load and display DIBs. The original image on the left is loaded at your computer's full resolution. Select a color resolution from the combo box and click the Copy button. The program converts the image into a DIB of the color resolution you selected. It draws some colored squares to show that it can manipulate the image's pixels. It then displays the results in the picture box on the right. In Figure 3.3, the program has downgraded an image from 24-bit color to 4-bit color. |
|
|
|
|
|
|
|
|
The previous sections explain how to manipulate device-dependent and device-independent bitmaps. For images that use more than 8 bits per pixel, the pixels map directly into color components. While the arithmetic involved in translating from bits to pixel components may be confusing for some color models, using 8 bits or fewer per pixel can be even more complicated. |
|
|
|
|
|
|
|
|
If you have the right hardware and enough memory, use 24-bit color. If you do not have enough memory, buy some more. Then you can skim the rest of this chapter just so that you know how much pain and trouble you are avoiding. |
|
|
|
|
|